<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/29
 * Time: 11:08
 */
namespace app\business\controller;

use think\Db;
use think\Session;

class Order extends Common
{
	/**
	 * 判断是否登录
	*/
	public function _initialize()
	{
		parent::_initialize();
	}
	
	/**
	 * 订单首页
	 * @param string get.title 商品名称
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('inventory')
			->field('shop.address,shop.id')
			->join('shop','shop.id = inventory.sid','LEFT')
			->where('bid',Session::get('business.id'))
			->whereNotNull('inventory.postid')
			->group('shop.address');
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('shop.address','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result =$db->paginate(8,false,$where);
		return view('index',compact('result'));
	}
	
	/**
	 * 详情
	 * @param string get.sid 店铺id
	 * @param string get.title 商品名称
	*/
	public function goods()
	{
		$where = ['query'=>[]];
		$sid = input('get.sid');
		$where['query']['sid'] = $sid;
		$db = Db::table('inventory')
			->field('product.pname,product.id as pid')
			->join('product','product.id = inventory.pid','LEFT')
			->where('inventory.sid',$sid)
			->where('inventory.bid',Session::get('business.id'))
			->whereNotNull('inventory.postid')
			//->group('product.pname')
			->order('inventory.id desc');
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('product.pname',$title);
			$where['query']['title'] = $title;
		}
		$result = $db->paginate(8,false,$where);
		return view('goods',compact('result','sid'));
	}
	
	/**
	 *@param string get.sid 店铺id
	 *@param string get.pid 商品id
	*/
	public function detail()
	{
		$sid = input('get.sid');
		$pid = input('get.pid');
		$count = Db::table('apply')->where('sid',$sid)
				->where('pid',$pid)->where('bid',Session::get('business.id'))
				->where('status',2)->count();
		if($count>0){
			$result = Db::table('inventory')
				->field('product.pname,inventory.sellnumber,inventory.number,sum(apply.number) as anumber,
						inventory.id,apply.status')
				->join('product',' product.id = inventory.pid ','LEFT')
				->join('apply',' inventory.postid = apply.postid ','RIGHT')
				->where('apply.pid',$pid)->where('apply.bid',Session::get('business.id'))
				->where('apply.status',2)->where('apply.sid',$sid)
				->group('product.pname')
				//->order('apply.id')
				->select();
		}else{
			$result = Db::table('inventory')
				->field('product.pname,inventory.sellnumber,inventory.number,inventory.id')
				->join('product',' product.id = inventory.pid ','LEFT')
				//->join('apply',' inventory.postid = apply.postid ','RIGHT')
				->where('inventory.pid',$pid)
				->where('inventory.bid',Session::get('business.id'))
				->where('inventory.sid',$sid)
				//->where('apply.status',1)
				//->where('inventory.sid',$sid)
				->group('product.pname')
				//->order('apply.id')
				->select();
		}
		return view('detail',compact('result'));
	}
	/**
	 * 申请结算
	 * @param string post.inventoryid 库存id
	 * @param string post.sellnumber 应结算的数量
	*/
	public function settlement()
	{
		if(request()->isAjax()){
			try{
				Db::startTrans();
				$inventoryid = input('post.inventoryid');
				$sellnumber = input('post.sellnumber');
				$date = time();
				$data = Db::table('inventory')->where('id',$inventoryid)->find();
				if($data){
					$input['bid'] = Session::get('business.id');
					$input['date'] = $date;
					$input['postid'] = $data['postid'];
					$input['sid'] = $data['sid'];
					$input['pid'] = $data['pid'];
					$input['number'] = $sellnumber;
					$info = Db::table('apply')->where('bid',$input['bid'])->where('sid',$input['sid'])
							->where('pid',$input['pid'])->where('status',1)->find();
					if($info){
						$date = time();
						$bool = Db::table('apply')->where('bid',$input['bid'])->where('sid',$input['sid'])
							->where('pid',$input['pid'])->where('status',1)->update(['number'=>$input['number'],'date'=>$date]);
						if(!$bool){
							throw new \PDOException('操作失败');
						}
					}else{
						$applyid = Db::table('apply')->insertGetId($input);
						if(!$applyid){
							throw new \PDOException('申请结算失败');
						}
					}
				}else{
					throw new \PDOException('订单已失效');
				}
				Db::commit();
				$success = ['success'=>'操作成功'];
				return json($success);
			}catch (\PDOException $e){
				Db::rollback();
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
}